


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: NamedQueryBuilder</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.datastore.database.querybuilder</a> ]
</div>

<h1>Coverage Summary for Class: NamedQueryBuilder (at.schauer.gregor.vienna.datastore.database.querybuilder)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">NamedQueryBuilder</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (9/ 9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    93.9%
  </span>
  <span class="absValue">
    (31/ 33)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.datastore.database.querybuilder;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.datastore.StorageMetadata;
<i>19</i>&nbsp;import org.apache.commons.lang3.ObjectUtils;
<i>20</i>&nbsp;import org.springframework.util.StringUtils;
<i>21</i>&nbsp;
<i>22</i>&nbsp;import javax.persistence.NamedQueries;
<i>23</i>&nbsp;import javax.persistence.NamedQuery;
<i>24</i>&nbsp;import javax.persistence.QueryHint;
<i>25</i>&nbsp;import java.util.HashMap;
<i>26</i>&nbsp;import java.util.Map;
<i>27</i>&nbsp;
<i>28</i>&nbsp;/**
<i>29</i>&nbsp; * @author Gregor Schauer
<i>30</i>&nbsp; */
<i>31</i>&nbsp;public class NamedQueryBuilder implements QueryBuilder {
<b class="fc"><i>32</i>&nbsp;	protected Map&lt;String, NamedQuery&gt; queryMap = new HashMap&lt;String, NamedQuery&gt;();</b>
<i>33</i>&nbsp;
<b class="fc"><i>34</i>&nbsp;	public NamedQueryBuilder(Class&lt;?&gt;... classes) {</b>
<b class="fc"><i>35</i>&nbsp;		register(classes);</b>
<b class="fc"><i>36</i>&nbsp;	}</b>
<i>37</i>&nbsp;
<i>38</i>&nbsp;	@Override
<i>39</i>&nbsp;	public String getQuery(String key) {
<b class="fc"><i>40</i>&nbsp;		NamedQuery namedQuery = getNamedQuery(key);</b>
<b class="fc"><i>41</i>&nbsp;		return namedQuery.query();</b>
<i>42</i>&nbsp;	}
<i>43</i>&nbsp;
<i>44</i>&nbsp;	@Override
<i>45</i>&nbsp;	public Object[] getParameters(String key, StorageMetadata&lt;?&gt; metadata) {
<b class="fc"><i>46</i>&nbsp;		if (READ.equals(key) || DELETE.equals(key)) {</b>
<b class="fc"><i>47</i>&nbsp;			return new Object[] {metadata.identifier};</b>
<i>48</i>&nbsp;		}
<b class="fc"><i>49</i>&nbsp;		return new Object[0];</b>
<i>50</i>&nbsp;	}
<i>51</i>&nbsp;
<i>52</i>&nbsp;	@Override
<i>53</i>&nbsp;	public Object[] getParameters(String key, Map&lt;String, Object&gt; map) {
<b class="fc"><i>54</i>&nbsp;		String[] parameterNames = getParameterNames(key);</b>
<b class="fc"><i>55</i>&nbsp;		Object[] parameters = new Object[parameterNames.length];</b>
<b class="fc"><i>56</i>&nbsp;		for (int i = 0; i &lt; parameterNames.length; i++) {</b>
<b class="fc"><i>57</i>&nbsp;			parameters[i] = map.get(parameterNames[i]);</b>
<i>58</i>&nbsp;		}
<b class="fc"><i>59</i>&nbsp;		return parameters;</b>
<i>60</i>&nbsp;	}
<i>61</i>&nbsp;
<i>62</i>&nbsp;	@Override
<i>63</i>&nbsp;	public String[] getParameterNames(String key) {
<b class="fc"><i>64</i>&nbsp;		NamedQuery namedQuery = getNamedQuery(key);</b>
<b class="fc"><i>65</i>&nbsp;		for (QueryHint hint : namedQuery.hints()) {</b>
<b class="fc"><i>66</i>&nbsp;			if (PARAMETER_NAMES.equals(hint.name())) {</b>
<b class="fc"><i>67</i>&nbsp;				return StringUtils.delimitedListToStringArray(hint.value(), &quot;,&quot;, &quot; &quot;);</b>
<i>68</i>&nbsp;			}
<i>69</i>&nbsp;		}
<b class="nc"><i>70</i>&nbsp;		return new String[0];</b>
<i>71</i>&nbsp;	}
<i>72</i>&nbsp;
<i>73</i>&nbsp;	protected NamedQuery getNamedQuery(String key) {
<b class="fc"><i>74</i>&nbsp;		NamedQuery query = queryMap.get(key);</b>
<b class="fc"><i>75</i>&nbsp;		if (query == null) {</b>
<b class="nc"><i>76</i>&nbsp;			throw new RuntimeException(key);</b>
<i>77</i>&nbsp;		}
<b class="fc"><i>78</i>&nbsp;		return query;</b>
<i>79</i>&nbsp;	}
<i>80</i>&nbsp;
<i>81</i>&nbsp;	protected NamedQueries getNamedQueries(Class&lt;?&gt; clazz) {
<b class="fc"><i>82</i>&nbsp;		NamedQueries namedQueries = clazz.getAnnotation(NamedQueries.class);</b>
<b class="fc"><i>83</i>&nbsp;		return namedQueries != null ? namedQueries : null;</b>
<i>84</i>&nbsp;	}
<i>85</i>&nbsp;
<i>86</i>&nbsp;	public void register(Class&lt;?&gt;... classes) {
<b class="fc"><i>87</i>&nbsp;		for (Class&lt;?&gt; clazz : ObjectUtils.defaultIfNull(classes, new Class[0])) {</b>
<b class="fc"><i>88</i>&nbsp;			NamedQueries namedQueries = getNamedQueries(clazz);</b>
<b class="fc"><i>89</i>&nbsp;			register(namedQueries);</b>
<i>90</i>&nbsp;		}
<b class="fc"><i>91</i>&nbsp;	}</b>
<i>92</i>&nbsp;
<i>93</i>&nbsp;	public void register(NamedQueries namedQueries) {
<b class="fc"><i>94</i>&nbsp;		if (namedQueries != null) {</b>
<b class="fc"><i>95</i>&nbsp;			for (NamedQuery namedQuery : namedQueries.value()) {</b>
<b class="fc"><i>96</i>&nbsp;				queryMap.put(namedQuery.name(), namedQuery);</b>
<i>97</i>&nbsp;			}
<i>98</i>&nbsp;		}
<b class="fc"><i>99</i>&nbsp;	}</b>
<i>100</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:23</div>
</div>
</body>
</html>
